package com.qboot.filestorage.service.impl;

import cn.xuyanwu.spring.file.storage.FileInfo;
import cn.xuyanwu.spring.file.storage.FileStorageService;
import cn.xuyanwu.spring.file.storage.platform.FileStorage;
import com.qboot.common.consts.MsgConst;
import com.qboot.common.entity.Result;
import com.qboot.common.utils.AssertUtils;
import com.qboot.filestorage.service.DefaultFileStorageService;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.PostConstruct;
import java.util.concurrent.CopyOnWriteArrayList;

/**
 * 对象存储 接口实现类
 *
 * @author Tellsea
 * @date 2023/2/17
 */
@Service
@RequiredArgsConstructor(onConstructor_ = @Autowired)
public class DefaultFileStorageServiceImpl implements DefaultFileStorageService {

    private final FileStorageService fileStorageService;

    @PostConstruct
    public void init() {
        CopyOnWriteArrayList<FileStorage> fileStorageList = fileStorageService.getFileStorageList();
        fileStorageList.forEach(item -> {
            System.out.println(item.getPlatform());
        });
    }

    @Override
    public Result upload(MultipartFile file) {
        AssertUtils.isEmpty(file, "文件对象不能为空");
        FileInfo fileInfo = fileStorageService.of(file).upload();
        AssertUtils.isEmpty(file, MsgConst.UPLOAD_ERROR);
        return Result.ok(MsgConst.UPLOAD_SUCCESS, fileInfo);
    }
}
